package site.wanjiahao.handler.client;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import site.wanjiahao.message.response.LoginResponseMessage;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

public class LoginResponseMessageHandler extends SimpleChannelInboundHandler<LoginResponseMessage> {
    private final AtomicBoolean SUCCESS;
    private final CountDownLatch AWAIT_SIGNAL;

    public LoginResponseMessageHandler(AtomicBoolean SUCCESS, CountDownLatch AWAIT_SIGNAL) {
        this.SUCCESS = SUCCESS;
        this.AWAIT_SIGNAL = AWAIT_SIGNAL;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginResponseMessage msg) throws Exception {
        // 线程间同步通信
        SUCCESS.set(msg.isSuccess());
        AWAIT_SIGNAL.countDown();
    }
}
